﻿
using System;
using System.Windows.Forms;
using System.Drawing;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace FaceFromCamera
{
    public partial class NewUser : Form,IView
    {
        PictureBox[] faces;
        int selectedPicture;
        bool editMode = false;
        int id_p;

        
        public NewUser()
        {
            InitializeComponent();
            selectedPicture = 0;
            faces = new PictureBox[5];
            for (int i = 0; i < faces.Length; i++)
            {
                faces[i] = new PictureBox();
                faces[i].Tag = i;
                faces[i].SizeMode = PictureBoxSizeMode.StretchImage;
                faces[i].Click += new EventHandler(Picture_Click);
                faces[i].Size = new Size(90, 90);
                faces[i].BackColor = Color.Black;
                flowLayoutPanel1.Controls.Add(faces[i]);
            }
        }
        public NewUser(Person p, Image[] savedFaces)
        {
            InitializeComponent();
            selectedPicture = 0;
            if (p.login == "admin")
            {
                comboBox1.Visible = true;
                comboBox1.SelectedIndex = -1;
                t_login.Visible = false;
                comboBox1.DataSource = PersonControler.Instance.GetUsernames();
            }
            else
            {


                faces = new PictureBox[savedFaces.Length];

                for (int i = 0; i < faces.Length; i++)
                {
                    faces[i] = new PictureBox();
                    faces[i].Tag = i;
                    faces[i].SizeMode = PictureBoxSizeMode.StretchImage;
                    faces[i].Click += new EventHandler(Picture_Click);
                    faces[i].Size = new Size(90, 90);
                    faces[i].BackColor = Color.Black;
                    faces[i].Image = savedFaces[i];
                    flowLayoutPanel1.Controls.Add(faces[i]);
                }
                t_login.Text = p.login;
                t_login.Enabled = false;
                t_name.Text = p.name;
                t_surname.Text = p.surname;
            }
            id_p = p.id;
            editMode = true;
        }

        
        
        
        #region IView Members
        public void ModelChange(object sender, Person e)
            {
                 preview.Image = RecognitionEngineControler.Instance.GetFaceFromFrame();
                 if (RecognitionEngineControler.Instance.GetFaceFromFrame() != null)
                 {
                     b_add.Enabled = true;
                 }
                 else
                 {
                     b_add.Enabled = false;
                 }
            }

       #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            //RecognitionEngineControler.Instance.RaiseModelChange(this, null);
        }

        private void NewUser_Load(object sender, EventArgs e)
        {
            RecognitionEngineControler.Instance.RegisterView(this);
            PersonControler.Instance.RegisterView(this);
        }

        private void b_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void b_add_Click(object sender, EventArgs e)
        {
            if (selectedPicture < faces.Length)
            {
                
                Image<Gray, byte> gray_faces = new Image<Gray, byte>(PictureOperations.ResizeImage(new Bitmap(preview.Image), 64, 64));
                if (gray_faces.Width == 64 && gray_faces.Height == 64)
                {
                    gray_faces._EqualizeHist();
                    faces[selectedPicture].Image = gray_faces.ToBitmap();
                    selectedPicture++;
                }
            }
            else
                selectedPicture = 0;
        }
        private void Picture_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < faces.Length; i++)
                faces[i].BorderStyle = BorderStyle.None;
            PictureBox gen = (PictureBox)sender;
            gen.BorderStyle = BorderStyle.Fixed3D;
            selectedPicture = (int)gen.Tag;
        }
        private bool emptyFace()
        {
            for (int i = 0; i < faces.Length; i++)
            {
                if (faces[i].Image == null)
                {
                    return true;
                }
            }
            return false;
        }
        Image[] faces_image()
        {
            Image[] temp = new Image[faces.Length];
            for (int i = 0; i < faces.Length; i++)
                temp[i] = faces[i].Image;
            return temp;
        }
        private void b_OK_Click(object sender, EventArgs e)
        {
            if (t_login.Text == "" || t_name.Text == "" || t_password.Text == "" || t_surname.Text == "" || emptyFace())
            {
                MessageBox.Show("You have to complete all fields\n and add 5 pictures of yourself!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

            }
            else
            {
                //this objects should be send to database
                if (editMode)
                {
                    PersonControler.Instance.Update(new Person(id_p, t_login.Text, t_password.Text, t_name.Text, t_surname.Text), faces_image());
                }
                else
                {
                    PersonControler.Instance.Save(new Person(0, t_login.Text, t_password.Text, t_name.Text, t_surname.Text), faces_image());
                }
                    this.Close();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            flowLayoutPanel1.Controls.Clear();
            string login = ((ComboBox)sender).Text;
            Person P = PersonControler.Instance.GetUser(login);
            id_p = P.id;
            t_name.Text = P.name;
            t_password.Text = P.password;
            t_surname.Text = P.surname;
            t_login.Text = P.login;

             Image[] savedFaces = PersonControler.Instance.UserPictures(id_p);

             faces = new PictureBox[savedFaces.Length];

             for (int i = 0; i < faces.Length; i++)
             {
                 faces[i] = new PictureBox();
                 faces[i].Tag = i;
                 faces[i].SizeMode = PictureBoxSizeMode.StretchImage;
                 faces[i].Click += new EventHandler(Picture_Click);
                 faces[i].Size = new Size(90, 90);
                 faces[i].BackColor = Color.Black;
                 faces[i].Image = savedFaces[i];
                 flowLayoutPanel1.Controls.Add(faces[i]);
             }

        }
    }
}
